package com.cskaoyan.service.configuration;

import com.cskaoyan.bean.bo.configuration.ExpressConfUpdateBo;
import com.cskaoyan.bean.bo.configuration.MallConfUpdateBo;
import com.cskaoyan.bean.bo.configuration.OrderConfUpdateBo;
import com.cskaoyan.bean.bo.configuration.WxConfUpdateBo;
import com.cskaoyan.bean.po.ohter.MarketSystem;
import com.cskaoyan.bean.po.ohter.MarketSystemExample;
import com.cskaoyan.bean.vo.configuration.ExpressConfListVo;
import com.cskaoyan.bean.vo.configuration.MallConfListVo;
import com.cskaoyan.bean.vo.configuration.OrderConfListVo;
import com.cskaoyan.bean.vo.configuration.WxConfListVo;
import com.cskaoyan.mapper.MarketSystemMapper;
import com.cskaoyan.util.common.Constant;
import com.cskaoyan.util.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @program: market
 * @description: 实现类装入组件容器
 * @author: zhong
 * @create: 2022-07-16 16:47
 **/
@Service
public class ConfigServiceImpl implements ConfigService {
    @Autowired
    MarketSystemMapper marketSystemMapper;

    private Object getConf(MarketSystemExample confType, Object confVO) {
        //检索,取出键值对
        List<MarketSystem> confList = marketSystemMapper.selectByExample(confType);
        HashMap<String, String> confMap = new HashMap<>();
        for (MarketSystem conf : confList) {
            confMap.put(conf.getKeyName(), conf.getKeyValue());
        }

        //返回对象赋值(键值对map→对象)
        Field[] fields = confVO.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (confMap.containsKey(field.getName())) {
                try {
                    field.setAccessible(true);
                    field.set(confVO, confMap.get(field.getName()));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return confVO;
    }

    private int updateConf(Object confBO) {
        //对象成员→键值对
        Field[] fields = confBO.getClass().getDeclaredFields();
        int updateRow = 0;
        for (Field field : fields) {
            //取键值
            field.setAccessible(true);
            String keyName = field.getName();
            String keyValue = null;
            try {
                keyValue = (String) field.get(confBO);
                //字符串校验
                if (StringUtils.isEmpty(keyValue)) {
                    return Constant.FORBIDDEN_CODE;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            //检查数据库是否需要更新
            MarketSystemExample conf = new MarketSystemExample();
            MarketSystemExample.Criteria reg = conf.createCriteria();
            reg.andDeletedEqualTo(false);
            reg.andKeyNameEqualTo(keyName);
            reg.andKeyValueEqualTo(keyValue);
            long count = marketSystemMapper.countByExample(conf);
            if (count == 1) {
                //无更新,跳过
                continue;
            }

            //更新数据库
            MarketSystem newConf = new MarketSystem();
            if (count == 0) {
                //有更新，添加键值，添加更新时间，new SimpleDateFormat("yyyy-MM-dd，HH:mm:ss").format(new Date())
                newConf.setKeyValue(keyValue);
                newConf.setUpdateTime(new Date());
            }
            MarketSystemExample oldConf = new MarketSystemExample();
            MarketSystemExample.Criteria oldConfReg = oldConf.createCriteria();
            oldConfReg.andDeletedEqualTo(false);
            oldConfReg.andKeyNameEqualTo(keyName);
            updateRow += marketSystemMapper.updateByExampleSelective(newConf, oldConf);
        }
        return updateRow;
    }

    @Override

    public MallConfListVo getMallConfOp() {
        //检索条件输入
        MarketSystemExample mall = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = mall.createCriteria();
        criteria.andKeyNameLike("market_mall%");
        criteria.andDeletedEqualTo(false);

        MallConfListVo mallConfListVo = new MallConfListVo();
        return (MallConfListVo) getConf(mall, mallConfListVo);
    }

    @Override
    public ExpressConfListVo getExpressConfOp() {
        //检索条件输入
        MarketSystemExample express = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = express.createCriteria();
        criteria.andKeyNameLike("market_express_freight%");
        criteria.andDeletedEqualTo(false);

        ExpressConfListVo expressConfListVo = new ExpressConfListVo();
        return (ExpressConfListVo) getConf(express, expressConfListVo);
    }

    @Override
    public OrderConfListVo getOrderConfOp() {
        //检索条件输入
        MarketSystemExample order = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = order.createCriteria();
        criteria.andKeyNameLike("market_order%");
        criteria.andDeletedEqualTo(false);

        OrderConfListVo orderConfListVo = new OrderConfListVo();
        return (OrderConfListVo) getConf(order, orderConfListVo);
    }

    @Override
    public WxConfListVo getWxConfOp() {
        //检索条件输入
        MarketSystemExample wx = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = wx.createCriteria();
        criteria.andKeyNameLike("market_wx%");
        criteria.andDeletedEqualTo(false);

        WxConfListVo wxConfListVo = new WxConfListVo();
        return (WxConfListVo) getConf(wx, wxConfListVo);
    }

    @Override
    public int changeMallConfOp(MallConfUpdateBo mallConf) {
        /**
         * todo:使用鉴权信息参数判定权限
         * if(){
         * return Constant.UNAUTHORIZED_CODE;}
         */
        int updateRow = updateConf(mallConf);
        if (updateRow == 6) {
            //全部数据更新
            return Constant.OK_CODE;
        } else if (updateRow == 0) {
            return Constant.NOT_FOUND_CODE;
        } else if (updateRow == Constant.FORBIDDEN_CODE) {
            return Constant.FORBIDDEN_CODE;
        } else {
            //更新了某几行
            return Constant.OK_CODE;
        }
    }

    @Override
    public int changeExpressConfOp(ExpressConfUpdateBo expressConf) {
        int updateRow = updateConf(expressConf);
        if (updateRow == 2) {
            return Constant.OK_CODE;
        } else if (updateRow == 0) {
            return Constant.NOT_FOUND_CODE;
        } else if (updateRow == Constant.FORBIDDEN_CODE) {
            return Constant.FORBIDDEN_CODE;
        } else {
            //更新了某几行
            return Constant.OK_CODE;
        }
    }

    @Override
    public int changeOrderConfOp(OrderConfUpdateBo orderConf) {
        int updateRow = updateConf(orderConf);
        if (updateRow == 3) {
            return Constant.OK_CODE;
        } else if (updateRow == 0) {
            return Constant.NOT_FOUND_CODE;
        } else if (updateRow == Constant.FORBIDDEN_CODE) {
            return Constant.FORBIDDEN_CODE;
        } else {
            //更新了某几行
            return Constant.OK_CODE;
        }
    }

    @Override
    public int changeWxConfOp(WxConfUpdateBo wxConf) {
        int updateRow = updateConf(wxConf);
        if (updateRow == 7) {
            return Constant.OK_CODE;
        } else if (updateRow == 0) {
            return Constant.NOT_FOUND_CODE;
        } else if (updateRow == Constant.FORBIDDEN_CODE) {
            return Constant.FORBIDDEN_CODE;
        } else {
            //更新了某几行
            return Constant.OK_CODE;
        }
    }


}
